package com.czy.owner.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.czy.owner.R;
import com.czy.owner.entity.MyServicePackageItemList;
import com.czy.owner.utils.MyLog;
import com.czy.owner.utils.glide.GlideUtils;
import com.czy.owner.widget.RoundImageView;

import java.util.List;

/**
 * Created by wangyang on 2016/12/19 0016.
 */
public class StorePackageDetailAdapter extends BaseRecyclerAdapter<MyServicePackageItemList, StorePackageDetailAdapter.StorePackageDetailHolder> {
    private boolean isMyPackage = false;
    private Context context;

    public StorePackageDetailAdapter(Context context, List<MyServicePackageItemList> list, boolean isMyPackage) {
        super(context, list);
        this.isMyPackage = isMyPackage;
        this.context = context;

    }

    @Override
    public void onBindViewHolder(StorePackageDetailHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        holder.bindData(data.get(position));
    }

    @Override
    public StorePackageDetailHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new StorePackageDetailHolder(layoutInflater.inflate(R.layout.item_package_detail, parent, false));
    }

    public class StorePackageDetailHolder extends BaseRecyclerAdapter.ViewHolder {
        private RoundImageView img_package_detail_icon;
        private TextView tv_package_detail_good_name;
        private TextView tv_package_detail_good_spec;
        private TextView tv_package_detail_good_money;
        private TextView tv_package_detail_good_num;
        private ImageView img_package_used;

        public StorePackageDetailHolder(View itemView) {
            super(itemView);
            img_package_detail_icon = (RoundImageView) itemView.findViewById(R.id.img_package_detail_icon);
            tv_package_detail_good_name = (TextView) itemView.findViewById(R.id.tv_package_detail_good_name);
            tv_package_detail_good_spec = (TextView) itemView.findViewById(R.id.tv_package_detail_good_spec);
            tv_package_detail_good_money = (TextView) itemView.findViewById(R.id.tv_package_detail_good_money);
            tv_package_detail_good_num = (TextView) itemView.findViewById(R.id.tv_package_detail_good_num);
            img_package_used = (ImageView) itemView.findViewById(R.id.img_package_used);


        }

        public void bindData(MyServicePackageItemList model) {
            if (isMyPackage) {
                String status = model.getStatus();
                if (status.equals("consumed")) {
                    img_package_used.setVisibility(View.VISIBLE);
                } else {
                    img_package_used.setVisibility(View.GONE);
                }
                if (!model.isUnlimitedCount()) {
                    tv_package_detail_good_num.setText("不限次");
                } else {
                    tv_package_detail_good_num.setText(model.getCount() + "/" + model.getTotalCount());
                }

            } else {
                if (!model.isUnlimitedCount()) {
                    tv_package_detail_good_num.setText("不限次");
                } else {
                    tv_package_detail_good_num.setText("x" + model.getCount());
                }
            }


            tv_package_detail_good_name.setText(model.getItemName());
            MyLog.e("yang", "model.getSpecName()==" + model.getSpecName());
            if (TextUtils.isEmpty(model.getSpecName())) {
                tv_package_detail_good_spec.setVisibility(View.GONE);
            } else {
                if (model.getSpecName().contains("规格:无规格")) {
                    tv_package_detail_good_spec.setVisibility(View.GONE);
                } else {
                    tv_package_detail_good_spec.setVisibility(View.VISIBLE);
                    tv_package_detail_good_spec.setText(model.getSpecName());
                }

            }


            tv_package_detail_good_money.setText("￥" + (String.format("%.2f", model.getPrice())));
            GlideUtils.loadImage(context, model.getLogo(), img_package_detail_icon, new GlideUtils.ImageLoadListener<String, GlideDrawable>() {
                @Override
                public void onLoadingComplete(String uri, ImageView view, GlideDrawable resource) {

                }

                @Override
                public void onLoadingError(String source, Exception e) {

                }
            }, R.mipmap.icon_default_package_detail, R.mipmap.icon_default_package_detail);

        }


    }
}
